package define

import (
	"fmt"
	"strings"
)

const (
	EventVersion = `v0.1`

	PushTypeMetrics = `metrics`
	PushTypeLogs    = `logs`
	PushTypeTraces  = `traces`

	EventCreate = iota
	EventDelete
	EventUpdate
	EventDescribe
	EventPushOn
	EventPushOff
)

func PushDataTypes() []string {
	return []string{PushTypeLogs, PushTypeMetrics}
}

type Tag struct {
	Key   string `json:"key" yaml:"key"`
	Type  string `json:"type" yaml:"type"`
	Value string `json:"value" yaml:"value"`
}

func (r *Tag) ToString() string {
	return fmt.Sprintf("%v|%v|%v", r.Key, r.Type, r.Value)
}

func (r *Tag) FromString(tStr string) error {
	res := strings.Split(tStr, "|")
	if len(res) != 3 {
		return fmt.Errorf("unkown tag string")
	}
	r.Key, r.Type, r.Value = res[0], res[1], res[2]
	return nil
}

type AgentResponse struct {
	State   bool   `json:"state" yaml:"state"`
	Message string `json:"message" yaml:"message"`
}

type AgentRequestHead struct {
	Version string `json:"version" yaml:"version"`
}

type AgentCreateObjectRequest struct {
	Head AgentRequestHead `json:"head" yaml:"head"`
	Type string           `json:"type" yaml:"type"`
	Guid string           `json:"guid" yaml:"guid"`
	Tags []Tag            `json:"tags" yaml:"tags"`
	Yaml []byte           `json:"yaml" yaml:"yaml"`
}

type AgentDeleteObjectRequest struct {
	Head AgentRequestHead `json:"head" yaml:"head"`
	Guid string           `json:"guid" yaml:"guid"`
}

type AgentUpdateObjectRequest struct {
	Head      AgentRequestHead       `json:"head" yaml:"head"`
	Guid      string                 `json:"guid" yaml:"guid"`
	Resources map[string]interface{} `json:"resouce" yaml:"resouce"`
}

type AgentPushMessageRequest struct {
	Head  AgentRequestHead `json:"head" yaml:"head"`
	Guid  string           `json:"guid" yaml:"guid"`
	Topic string           `json:"topic" yaml:"topic"`
	PushT string           `json:"pushType" yaml:"pushType"`
}

type AgentPushMetricRequest struct {
	Guid  string `json:"guid" yaml:"guid"`
	Topic string `json:"topic" yaml:"topic"`
}

type AgentPushMetricResponse struct {
	Code    string `json:"code" yaml:"code"`
	Message string `json:"message" yaml:"message"`
	Data    string `json:"data" yaml:"data"`
}

type JaegerPushTraceRequest struct {
	Guid  string `json:"guid" yaml:"guid"`
	Topic string `json:"topic" yaml:"topic"`
}

type JaegerPushTraceResponse struct {
	Code    string `json:"code" yaml:"code"`
	Message string `json:"message" yaml:"message"`
	Data    string `json:"data" yaml:"data"`
}
